package com.yuanmei.charts;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;

import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.yuanmei.views.R;
import com.yuanmei.common.Utils;

import java.util.ArrayList;
import java.util.List;

public class RSIChartDataSet implements TaChartData {
//    private static final String TAG = RSIChartDataSet.class.getName();
    private List<Entry> mRSI5List;
    private List<Entry> mRSI10List;
    private List<Entry> mRSI20List;
    private List<BarEntry> mZBarList;

    private List<String> mXAxisDates;
    private CombinedData mRSICombineData;
    private Context mContext;
    private KLineChartDataSet mRawDataSet;

    public RSIChartDataSet(Context context, KLineChartDataSet rawDataSet) {
        mContext = context;
        mRawDataSet = rawDataSet;

        mRSI5List = new ArrayList<>();
        mRSI10List = new ArrayList<>();
        mRSI20List = new ArrayList<>();
        mZBarList = new ArrayList<>();
        mXAxisDates = new ArrayList<>();

        mRSI5List.add(new Entry(0, 50));
        mRSI10List.add(new Entry(0,50));
        mRSI20List.add(new Entry(0,50));

        float rise5Sum = 0, drop5Sum = 0;
        float rise10Sum = 0, drop10Sum = 0;
        float rise20Sum = 0, drop20Sum = 0;
        for (int i = 1; i < mRawDataSet.getXAxisSize(); i++) {
            mXAxisDates.add(mRawDataSet.getXAxisLabel(i));
            float close = mRawDataSet.getClose(i);
            float preClose = mRawDataSet.getClose(i - 1);
            float diff = close - preClose;
            if (diff > 0) {
                rise5Sum += diff;
                rise10Sum += diff;
                rise20Sum += diff;
            } else if (diff < 0) {
                drop5Sum += Math.abs(diff);
                drop10Sum += Math.abs(diff);
                drop20Sum += Math.abs(diff);
            }

            if (i > 5) {
                int rmIdx = i - 5;
                float p5diff = mRawDataSet.getClose(rmIdx) - mRawDataSet.getClose(rmIdx - 1);
                if (p5diff > 0) {
                    rise5Sum -= p5diff;
                } else if (p5diff < 0) {
                    drop5Sum -= Math.abs(p5diff);
                }
             }
             if (i > 10) {
                int rmIdx = i - 10;
                float p10diff = mRawDataSet.getClose(rmIdx) - mRawDataSet.getClose(rmIdx - 1);
                if (p10diff > 0) {
                    rise10Sum -= p10diff;
                } else if (p10diff < 0) {
                    drop10Sum -= Math.abs(p10diff);
                }
             }

             if (i > 20) {
                int rmIdx = i - 20;
                float p20diff = mRawDataSet.getClose(rmIdx) - mRawDataSet.getClose(rmIdx - 1);
                if (p20diff > 0) {
                    rise20Sum -= p20diff;
                } else if (p20diff < 0) {
                    drop20Sum -= Math.abs(p20diff);
                }
             }

            float rsi5 = 100 * (rise5Sum / (rise5Sum + drop5Sum));
            float rsi10 = 100 * (rise10Sum / (rise10Sum + drop10Sum));
            float rsi20 = 100 * (rise20Sum / (rise20Sum + drop20Sum));
            mRSI5List.add(new Entry(i, rsi5));
            mRSI10List.add(new Entry(i, rsi10));
            mRSI20List.add(new Entry(i, rsi20));

            mZBarList.add(new BarEntry(i, 0));
        }

        BarDataSet barSet = new BarDataSet(mZBarList, "");
        barSet.setDrawValues(false);
        BarData barData = new BarData(barSet);

        ArrayList<ILineDataSet> lineDataSet = new ArrayList<>();
        LineDataSet rsi5Set =  new LineDataSet(mRSI5List, "RSI5");
        TaLib.setCommonMaStyle(rsi5Set);
        rsi5Set.setHighlightEnabled(true);
        rsi5Set.setDrawHorizontalHighlightIndicator(true);
        rsi5Set.setDrawVerticalHighlightIndicator(true);
        rsi5Set.setHighLightColor(mContext.getResources().getColor(R.color.border_color));
        rsi5Set.setColor(mContext.getResources().getColor(R.color.red));
        lineDataSet.add(rsi5Set);

        LineDataSet rsi10Set =  new LineDataSet(mRSI10List, "RSI10");
        TaLib.setCommonMaStyle(rsi10Set);
        rsi10Set.setColor(mContext.getResources().getColor(R.color.yellow));
        lineDataSet.add(rsi10Set);

        LineDataSet rsi20Set =  new LineDataSet(mRSI20List, "RSI20");
        TaLib.setCommonMaStyle(rsi20Set);
        rsi20Set.setColor(mContext.getResources().getColor(R.color.blue));
        lineDataSet.add(rsi20Set);

        LineData linesData = new LineData(lineDataSet);

        mRSICombineData = new CombinedData();
        mRSICombineData.setData(barData);
        mRSICombineData.setData(linesData);
    }

    @Override
    public int getXAxisSize() {
        return mXAxisDates.size();
    }

    @Override
    public String getXAxisLabel(int index) {
        String date = "None";
        if (index >= 0 && index < mXAxisDates.size()) {
            date = mXAxisDates.get(index);
        }
        return date;
    }

    @Override
    public Spanned getDescription(int index) {
        String rsi5 = "5:";
        if (index >= 0 && index < mRSI5List.size()) {
            rsi5 += Utils.priceToDecimalString(mRSI5List.get(index).getY());
        }

        String rsi10 = "  10:";
        if (index >= 0 && index < mRSI10List.size()) {
            rsi10 += Utils.priceToDecimalString(mRSI10List.get(index).getY());
        }

        String rsi20 = " 20:";
        if (index >= 0 && index < mRSI20List.size()) {
            rsi20 += Utils.priceToDecimalString(mRSI20List.get(index).getY());
        }
        // "<font color=\"#FE6026\">" + "Test" + "</font>"
        String str1 = String.format(
                "RSI: <font color='#FF0000'>%s</font> <font color='#FFD700'>%s</font> <font color='#00b2ee'>%s</font>",
                rsi5, rsi10, rsi20);
        return  Html.fromHtml(str1);
    }

    @Override
    public List<String> getXAxisLabelList() {
        return mXAxisDates;
    }

    @Override
    public CombinedData getTaChartData() {
        return mRSICombineData;
    }
}
